#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>

#define FOR(i,a,n) for(int i=a;i<n;++i)
#define REP(i,n) FOR(i,0,n)

using namespace std;

int dp[200200][2];
const int inf = 1<<29;

int main()
{
    int n;
    while(cin>>n)
    {
        REP(i,200000)dp[i][0]=dp[i][1]=-inf;
        dp[100000][0]=dp[100000][1]=0;
        vector<bool> done(200000,false);
        vector<int> list;
        list.push_back(100000);
        done[100000]=true;
        int ans=0;
        int flag = 0;
        REP(i,n)
        {
            int si,fi;
            scanf("%d%d",&si,&fi);
            int size=list.size();
            REP(j,size)
            {
                int ns = list[j];
                if(ns+si<0||ns+si>=200000) continue;
                dp[ns][flag^1]=max(dp[ns][flag],dp[ns][flag^1]);
                if(!done[ns+si])
                {
                    list.push_back(ns+si);
                    dp[ns+si][flag^1]=dp[ns][flag]+fi;
                    done[ns+si]=true;
                }
                else
                {
                    dp[ns+si][flag^1]=max(dp[ns+si][flag],dp[ns][flag]+fi);
                }
                if(ns+si>=100000&&dp[ns+si][flag^1]>=0) ans=max(ans,ns+si+dp[ns+si][flag^1]-100000);
            }
            flag^=1;
        }
        printf("%d\n",ans);
    }
	return 0;
}


